package jdbcmapper;

import java.util.Hashtable;
import java.util.Vector;

import lotus.domino.*;
/**
 * <p>Title: JDBC Mapper</p>
 *
 * <p>Description: Libraries to copy documents form Lotus Notes to jdbc
 * table</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: The Salvation ARmy</p>
 *
 * @author Ansel Robateau
 * @version 1.1
 */
public class NotesForm extends NotesDoc {
    private NotesDB m_parent;
    private Hashtable sqldatabases;
    public static final String NOTESFORM = "NotesForm";
    public static final String NOTESDATABASE = "NotesDatabase";
    public static final String SQLDATABASE = "SQLDatabase";
    public static final String SQLTABLE = "SQLTable";
    public static final String date_LASTACCESSED = "LastAccessed";
    public NotesForm(Session session,Document doc) {
        super(session,doc);
        this.sqldatabases = new Hashtable();
    }
    public SQLDB getFirstSQLDB(){
        return (SQLDB)this.getFirst();
    }
    public SQLDB getNextSQLDB(){
        return (SQLDB)this.getNext();
    }
    public NotesDB getParent(){
        return this.m_parent;
    }
    /**
     * LoadMapping
     */
    public void LoadMapping(NotesDB db,FieldMap map) throws Exception {
        this.m_parent = db;
        String sqldatabase = map.getString(FieldMap.SQLDATABASE);
        try {
            if(sqldatabases.get(sqldatabase) == null){
                throw new Exception();
            }
        } catch (Exception e) {
            //View view = this.getDocument().getParentDatabase().getView("SQL Database Connections");
            //ViewEntryCollection vc = view.getAllEntriesByKey(map.getString(FieldMap.SQLDATABASE));
            //ViewEntry entry = vc.getFirstEntry();
            String str = "Form = \"Database Connection\" & "+
                    "SQLDatabase = \"" + map.getString(map.SQLDATABASE) + "\"";
            //System.out.println(str);
            this.getM_log().add(str);
            DocumentCollection col = this.getDocument().getParentDatabase().search(str);
            //System.out.println("Documents found = " + col.getCount());
            this.getM_log().add("Documents found = " + col.getCount());
            Document doc = col.getFirstDocument();
            if(doc != null){
                //doc = entry.getDocument();
                SQLDB sqldb = new SQLDB(this.getSession(),doc);
                this.m_subs.add(sqldb);
                sqldb.LoadMapping(this,map);
                sqldatabases.put(sqldatabase, "");
            }
        }
    }

    /**
     * getName
     *
     * @return String
     */
    public String getName() throws Exception {
        return this.getString(NotesForm.NOTESFORM);
    }

    public Vector getM_log() {
        return this.getParent().getM_log();
    }
}
